package com.crm.controller.adminController;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.ExecutivesDAO;
import com.crm.entity.Business;
import com.crm.entity.Executives;

@Controller
@RequestMapping("/executives")
public class ExecutivesController {
	protected final Log logger = LogFactory.getLog(getClass());
	ExecutivesDAO exDAO=new ExecutivesDAO();
	List<Executives> executivesList=new ArrayList<Executives>();
	Executives executives=null;
	
	@RequestMapping("/show.do")
	public ModelAndView show(HttpServletRequest request,HttpServletResponse response,HttpSession session,ModelAndView model){
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		int busid=bus.getBusinessId();
		
		executivesList=new ArrayList<Executives>();
		try {
			executivesList = exDAO.findAll(busid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return model=new ModelAndView("/jsp/Executives.jsp")
		.addObject("menuName","admin")
		.addObject("submenu","admin")
		.addObject("execList",executivesList);
	}
	@RequestMapping("/saveExecutive.do")
	public ModelAndView saveExecutive(HttpServletRequest request,HttpServletResponse response,HttpSession session,ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		
		
		String name=request.getParameter("execname");
		String country=request.getParameter("country");
		String bloodGroup=request.getParameter("bloodGroup");
		String gender=request.getParameter("gender");
		String email=request.getParameter("email");
		String fax=request.getParameter("fax");
		String sdob=request.getParameter("dob");
		String sjoiningDate=request.getParameter("joiningDate");		
		String phone=request.getParameter("phone");
		String cell=request.getParameter("cell");
		String address=request.getParameter("address");
					
		DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
		Date  dob = formatter.parse(sdob);
		Date  joiningDate = formatter.parse(sjoiningDate);
		
		
		
		executives=new Executives(bus,name,email,phone,address,fax,country,cell,bloodGroup,gender,new java.sql.Date(dob.getTime()),new java.sql.Date(joiningDate.getTime()));
		String msg=null;
		try{
			exDAO.insert(executives);
			msg="Succesfully Saved Executive Information";
		}catch(Exception ex){
			ex.printStackTrace();
			msg= "Failed To Save Executive Information.Please Try Again";
		}
		return model=new ModelAndView("/jsp/Executives.jsp")
		.addObject("menuName","admin")
		.addObject("submenu","admin")
		.addObject("msg", msg);
	}
	@RequestMapping("/edit.do")
	public ModelAndView edit(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		executivesList=new ArrayList<Executives>();
		executivesList=exDAO.findAll(bus.getBusinessId());
		logger.info("Executives List In Controller Is:"+executivesList);
		model.addObject("executivesList",executivesList);
		model.addObject("submenu", "admin");
		model.addObject("menuName","admin");
		model.setViewName("/jsp/Executives.jsp");
		return model;
		}
	@RequestMapping("/search.do")
	public ModelAndView search(HttpServletRequest request,HttpServletResponse response,HttpSession session,ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		int busid=bus.getBusinessId();
		executives=new Executives();
		String execname=request.getParameter("execName");;
		executives=exDAO.findByName(execname, busid);
		logger.info("Executive In Controller Is:"+executives);
		model.setViewName("/jsp/editSources.jsp");
		return model=new ModelAndView("/jsp/editExecutives.jsp")
		.addObject("menuName","admin")
		.addObject("submenu","admin")
		.addObject("executive", executives);
	}
	@RequestMapping("/getAllExec.do")
	public @ResponseBody String getAllExec(HttpServletRequest request,HttpServletResponse response,HttpSession session,ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		executivesList=new ArrayList<Executives>();
		executivesList=exDAO.findAll(bus.getBusinessId());
		StringBuilder sb=new StringBuilder();
		for(Executives ex:executivesList){
			sb.append(ex.getExeName()+"@"+ex.getId()+",");
		}
		return sb.toString();
	}
	@RequestMapping("/delete.do")
	public ModelAndView delete(HttpServletRequest request,HttpServletResponse response,HttpSession session,ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		String sexecId=request.getParameter("execId");
		if(sexecId!=null){
			int execId=Integer.parseInt(sexecId);
			exDAO.deleteExecutive(bus.getBusinessId(),execId);
		}
		return new ModelAndView("redirect:show.do","msg","Successfully Deleted Executive");
	}
	@RequestMapping("/editSave.do")
	public ModelAndView editsave(HttpServletRequest request,HttpServletResponse response,HttpSession session,ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		
		String sid=request.getParameter("id");
		String name=request.getParameter("execname");
		String country=request.getParameter("country");
		String bloodGroup=request.getParameter("bloodGroup");
		String gender=request.getParameter("gender");
		String email=request.getParameter("email");
		String fax=request.getParameter("fax");
		String sdob=request.getParameter("dob");
		String sjoiningDate=request.getParameter("joiningDate");		
		String phone=request.getParameter("phone");
		String cell=request.getParameter("cell");
		String address=request.getParameter("address");
		int id=Integer.parseInt(sid);
		logger.info("Id Value Is"+sid);
		DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
		java.util.Date  dob = formatter.parse(sdob);
		java.util.Date  joiningDate = formatter.parse(sjoiningDate);
		
		logger.info("DOB After Formatting Is:"+dob);
		executives=new Executives(id,bus,name,email,phone,address,fax,country,cell,bloodGroup,gender,new java.sql.Date(dob.getTime()),new java.sql.Date(joiningDate.getTime()));
		executives.setId(id);
		logger.info("Executive Details Are"+executives);
		String msg=null;
		try{
			exDAO.insert(executives);
			msg="Successfully Updated Executive Details";
		}catch(Exception ex){
			ex.printStackTrace();
			msg="Failed  To Update Executive Details.Please Try Again";
		}
		
		
		return model=new ModelAndView("/jsp/Executives.jsp")
		.addObject("menuName","admin")
		.addObject("submenu","admin")
		.addObject("msg",msg);
	}
}
